library AIDS requires UnitDex

    public function RegisterOnEnter takes boolexpr b returns triggercondition
        return RegisterUnitIndexEvent(b, EVENT_UNIT_INDEX)
    endfunction

    public function RegisterOnEnterAllocated takes boolexpr b returns triggercondition
        return RegisterUnitIndexEvent(b, EVENT_UNIT_INDEX)
    endfunction

    public function RegisterOnDeallocate takes boolexpr b returns triggercondition
        return RegisterUnitIndexEvent(b, EVENT_UNIT_DEINDEX)
    endfunction

    function GetIndexUnit takes integer index returns unit
        return GetUnitById(index)
    endfunction

    function GetUnitIndex takes unit u returns integer
        return GetUnitId(u)
    endfunction

    public struct DEFAULT extends array
        method AIDS_onCreate takes nothing returns nothing
        endmethod

        method AIDS_onDestroy takes nothing returns nothing
        endmethod
      
        static method AIDS_filter takes unit u returns boolean
            return true
        endmethod
      
        static method AIDS_onInit takes nothing returns nothing
        endmethod
    endstruct

    //! textmacro AIDS
        private static delegate AIDS_DEFAULT deleg = 0

        method AIDS_addLock takes nothing returns nothing
        endmethod

        method AIDS_removeLock takes nothing returns nothing
        endmethod

        static method operator[] takes unit whichUnit returns thistype
            return GetUnitId(whichUnit)
        endmethod

        method operator unit takes nothing returns unit
            return GetUnitById(this)
        endmethod

        private static method AIDS_onEnter takes nothing returns boolean
            call thistype(GetIndexedUnitId()).AIDS_onCreate()
            return false
        endmethod

        private static method AIDS_onDeallocate takes nothing returns boolean
            if IsUnitIndexed(GetIndexedUnit())  then
                call thistype(GetIndexedUnitId()).AIDS_onDestroy()
            endif
            return false
        endmethod

        private static method onInit takes nothing returns nothing
            call RegisterUnitIndexEvent(Filter(function thistype.AIDS_onEnter), EVENT_UNIT_INDEX)
            call RegisterUnitIndexEvent(Filter(function thistype.AIDS_onDeallocate), EVENT_UNIT_DEINDEX)

            call thistype.AIDS_onInit()
        endmethod
    //! endtextmacro

endlibrary